package cq.com.cn.dao;

import cq.com.cn.model.BooksModel;
import cq.com.cn.model.UserModel;
import cq.com.cn.utils.JDBCUtils;

import java.util.List;

public class BooksDao {
    //查询所有书籍
    private static  final String findAllBook="select * from books";
    //根据书籍类别查询对应的书籍
    private static  final String findAllBookByType="select * from books where bookType=?";
    //根据书籍名称查询对应的书籍
    private static  final String findAllBookByName="select * from books where bookName like ? ";

    private static final String FINDBOOKBYID="select * from books where id= ?";
    /**
     * @param type 书籍类别
     * @return 根据type查询书籍
     */
    public List<BooksModel> queryBookByType(int type){
        if(type==0)
            return JDBCUtils.findList(BooksModel.class,findAllBook,new Object[]{});
        else
            return JDBCUtils.findList(BooksModel.class,findAllBookByType,type);
    }

    public List<BooksModel> queryBookByName(String bookName) {

        return JDBCUtils.findList(BooksModel.class,findAllBookByName,"%"+bookName.trim()+"%");
    }

    public BooksModel queryBookById(String id){
        return JDBCUtils.findOne(BooksModel.class,FINDBOOKBYID,id);
    }
}
